/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.usuallyinterface.impl;

import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.database.dao.WmsWarehouseMapper;
import com.lightkits.wms.database.model.KVCodeModel;
import com.lightkits.wms.domain.usuallyinterface.GetQuickCodeService;
import com.lightkits.wms.dto.usuallyinterface.model.QuickCodeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jing.wei@hand-china.com
 * @date 2019/5/31 7:32
 **/
@Service
public class GetQuickCodeServiceImpl implements GetQuickCodeService {
    private static final Logger LOGGER = LoggerFactory.getLogger(GetQuickCodeServiceImpl.class);


    private final WmsWarehouseMapper wmsWarehouseMapper;

    @Autowired
    public GetQuickCodeServiceImpl(WmsWarehouseMapper wmsWarehouseMapper) {
        this.wmsWarehouseMapper = wmsWarehouseMapper;
    }

    /**
     * @param code    快速编码值
     * @param value   该快速编码下的编码含义值
     * @param meaning 快速编码的展示值
     * @return QuickCodeDto
     **/
    @Override
    public CheckResult<List<QuickCodeDto>> getQuickCode(String code, String value, String meaning) {
        CheckResult<List<QuickCodeDto>> checkResult = new CheckResult<>();
        checkResult.setSuccessful(true);
        try {
            if (ObjectUtil.isNullOrEmpty(code)) {
                checkResult.setSuccessful(false);
                checkResult.setMessage("the Code is Empty");
                return checkResult;
            }
            List<QuickCodeDto> quickCodeDtos = new ArrayList<QuickCodeDto>();
            List<KVCodeModel> models = wmsWarehouseMapper.querySysCode(code, value, meaning);
            models.forEach(model -> {
                QuickCodeDto dto = new QuickCodeDto();
                dto.setValue(model.getCode());
                dto.setMeaning(model.getValue());
                dto.setDescription(model.getDescription());
                quickCodeDtos.add(dto);
            });
            checkResult.setData(quickCodeDtos);
        } catch (Exception e) {
            LOGGER.error("getQuickCode.error", e);
        }
        return checkResult;
    }
}
